'use strict';
/**
 * 路由
 * Created by zhaijg.
 */
angular.module('app')
  .run(
    [          '$rootScope', '$state', '$stateParams','$cookieStore',
      function ($rootScope,   $state,   $stateParams,$cookieStore) {
          $rootScope.$state = $state;
          $rootScope.$stateParams = $stateParams;
          $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
              if(toState.name=='access.signin'){// 如果是进入登录界面则允许
                  $cookieStore.remove("lingjin_user");
                  $rootScope.user = null;
                  clearInterval($rootScope.setTimeValue);
                  return;
              }
              if(!$rootScope.user){
                  if($cookieStore.get("lingjin_user")!=undefined){
                      $rootScope.user = $cookieStore.get("lingjin_user");
                      app.token = $rootScope.user.token;
                      $rootScope.initData();
                  }
              }
              // 如果用户不存在
              if(!$rootScope.user){
                  event.preventDefault();// 取消默认跳转行为
                  $state.go("access.signin",{});//跳转到登录界面
              }
          });
      }
    ]
  )
  .config(
    [          '$stateProvider', '$urlRouterProvider','JQ_CONFIG',
      function ($stateProvider,   $urlRouterProvider,JQ_CONFIG) {

          $urlRouterProvider
              .otherwise('/app/user/auth');
          $stateProvider
              .state('app', {
                  abstract: true,
                  url: '/app',
                  templateUrl: 'tpl/app.html'
              })
              // user 会员管理
              .state('app.user', {
                  url: '/user',
                  template: '<div ui-view></div>'
              })
              //例程
              .state('app.user.list', {
                  url: '/list',
                  controller:"userListCtrl",
                  templateUrl: 'tpl/user/list.html',
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'].concat(JQ_CONFIG['datepicker']))
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/userListCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //用户列表
              .state('app.user.auth', {
                  url: '/auth',
                  templateUrl: 'tpl/user/authList.html',
                  controller:"userAuthListCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'])
                                  .then($ocLazyLoad.load('ui.tree'))
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then($ocLazyLoad.load('angular-clipboard'))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/userAuthListCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //会员名片管理
              .state('app.user.card', {
                  url: '/card',
                  templateUrl: 'tpl/user/cardList.html',
                  controller:"userCardListCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'])
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/userCardListCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //用户邀请管理
              .state('app.user.invite', {
                  url: '/invite',
                  templateUrl: 'tpl/user/invite_list.html',
                  controller:"userInviteCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load(JQ_CONFIG['dataTable']
                                  .concat(JQ_CONFIG['datetimepicker']))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/userInviteCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //用户分类
              .state('app.user.cat', {
                  url: '/cat',
                  templateUrl: 'tpl/user/cat_list.html',
                  controller:"userCatCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return  $ocLazyLoad.load('ui.tree')
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/userCatCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //用户登陆历史
              .state('app.user.login', {
                  url: '/login',
                  templateUrl: 'tpl/user/user_login.html',
                  controller:"userLoginCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load(JQ_CONFIG['dataTable'])
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/userLoginCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              // need 需求管理
              .state('app.need', {
                  url: '/need',
                  template: '<div ui-view></div>'
              })
              //app2.2 需求审核
              .state('app.need.check', {
                  url: '/check',
                  templateUrl: 'tpl/need/check.html',
                  controller:"needCheckCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return $ocLazyLoad.load('ngFileUpload')
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/needCheckCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //原始需求列表
              .state('app.need.resolve', {
                  url: '/resolve',
                  templateUrl: 'tpl/need/olist.html',
                  controller:"oneedListCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'])
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/oneedListCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //需求拆分
              .state('app.need.split', {
                  url: '/split/:needId/:from',
                  templateUrl: 'tpl/need/needModal.html',
                  controller:"needInfoModalCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'].concat(JQ_CONFIG['datepicker']))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/oneedListCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //需求更新列表
              .state('app.need.update', {
                  url: '/update',
                  templateUrl: 'tpl/need/updatelist.html',
                  controller:"needUpdateCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'].concat(JQ_CONFIG['datepicker']))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/needUpdateCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //需求更新
              .state('app.need.splitUp', {
                  url: '/splitUp/:needIndex/:requestId',
                  templateUrl: 'tpl/need/updateInfor.html',
                  controller:"updateInfor",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'].concat(JQ_CONFIG['datepicker']))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/needUpdateCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //微信聊天记录
              .state('app.need.wxmsg', {
                  url: '/wxmeg',
                  templateUrl: 'tpl/need/view_chat_log.html',
                  controller:"viewChatLogCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['datetimepicker'])
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/viewChatLogCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              // need 需求管理
              .state('app.need.match', {
                  url: '/match',
                  template: '<div ui-view></div>',
                  cache:true,
                  controller:"needMatchCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'].concat(JQ_CONFIG['datepicker']))
                                  .then(function(){
                                      return uiLoad.load( [
                                          'js/controllers/needMatchCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //用户需求申请
              .state('app.need.request', {
                  url: '/request',
                  templateUrl: 'tpl/need/need_request.html',
                  controller:"needRequestCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'])
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/needRequestCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //需求撮合
              .state('app.need.match.index', {
                  url: '/index',
                  cache:true,
                  templateUrl: 'tpl/need/index.html'
              })
              //需求撮合
              .state('app.need.match.search', {
                  url: '/search',
                  cache:true,
                  templateUrl: 'tpl/need/search.html'

              })
              // cat 分类管理
              .state('app.cat', {
                  url: '/cat',
                  template: '<div ui-view></div>'
              })
              //分类管理
              .state('app.cat.list', {
                  url: '/list',
                  templateUrl: 'tpl/cat/list.html',
                  controller:"catListCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              //return uiLoad.load(JQ_CONFIG['dataTable'].concat(JQ_CONFIG['easyui']))
                              return  $ocLazyLoad.load('ui.tree')
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then($ocLazyLoad.load('multi-select-tree'))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/catListCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //增加访问量管理
              .state('app.cat.AddPvAdmin', {
                  url: '/AddPvAdmin',
                  templateUrl: 'tpl/cat/AddPvAdmin.html',
                  controller:"catNumListCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'])
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/catNumListCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              // 管理员用
              .state('app.card', {
                  url: '/card',
                  template: '<div ui-view></div>'
              })
              //名片列表
              .state('app.card.list', {
                  url: '/list/:uid',
                  templateUrl: 'tpl/user/user_card_list.html',
                  controller:"cardListCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load( ['js/controllers/cardListCtrl.js'] );
                          }]
                  }
              })
              // 运营用
              .state('app.op', {
                  url: '/op',
                  template: '<div ui-view></div>'
              })
              //推荐管理
              .state('app.op.recommend', {
                  url: '/recommend',
                  templateUrl: 'tpl/op/recommend.html',
                  controller:"opListCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable']
                                  .concat(JQ_CONFIG['datetimepicker']))
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/opListCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //运营用户录入
              .state('app.op.userInput', {
                  url: '/userInput',
                  templateUrl: 'tpl/op/userInput.html',
                  controller:"userInputCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['dataTable'].concat(JQ_CONFIG['datepicker']))
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/opUserInputCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //app版本控制
              .state('app.op.appVersion', {
                  url: '/appVersion',
                  templateUrl: 'tpl/op/app_version.html',
                  controller:"opAppversionCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load( ['js/controllers/opAppversionCtrl.js'] );
                          }]
                  }
              })
              //活动管理
              .state('app.op.activity', {
                  url: '/activity',
                  templateUrl: 'tpl/activity/list.html',
                  controller:"activityCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad ){
                              return uiLoad.load(JQ_CONFIG['datetimepicker'])
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then(function () {
                                      return uiLoad.load( ['js/controllers/activityCtrl.js'] );
                                  })
                          }]
                  }
              })
              //运营信息展示
              // uid 用户id
              // enterType: 1 运营管理进入(app.op.userInput) 2 交易撮合(app.need.match),
              // showType: 1 显示用户基本信息 2 用户基本信息 3 用户基本信息
              // actionType : 1 更新 2 查看
              .state('app.op.usershow', {
                  url: '/usershow/:uid/:enterType/:showType/:actionType',
                  templateUrl: 'tpl/op/usershow.html',
                  controller:"usershowCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load(JQ_CONFIG['dataTable']
                                .concat(JQ_CONFIG['datepicker']))
                                .then(function(){
                                    return uiLoad.load( [
                                        'js/controllers/usershowCtrl.js',
                                        'js/controllers/usershowUserCtrl.js',
                                        'js/controllers/usershowNeedCtrl.js',
                                        'js/controllers/usershowTraceCtrl.js'] );
                                }
                            );
                          }]
                  }
              })
              //feed管理
              .state('app.op.feed', {
                  url: '/feed',
                  templateUrl: 'tpl/feed/account_list.html',
                  controller:"feedPushCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return uiLoad.load(JQ_CONFIG['datetimepicker'])
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then($ocLazyLoad.load('multi-select-tree'))
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/feedPushCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              // 管理员
              .state('app.admin', {
                  url: '/admin',
                  template: '<div ui-view></div>'
              })
              //管理员列表
              .state('app.admin.list', {
                  url: '/list',
                  templateUrl: 'tpl/admin/admin_list.html',
                  controller:"adminListCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load( ['js/controllers/adminListCtrl.js'] );
                          }]
                  }
              })
              //角色列表
              .state('app.admin.role', {
                  url: '/role',
                  templateUrl: 'tpl/admin/role_list.html',
                  controller:"adminRoleCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return $ocLazyLoad.load('ui.tree')
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/adminRoleCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //部门列表
              .state('app.admin.department', {
                  url: '/department',
                  templateUrl: 'tpl/admin/department_list.html',
                  controller:"adminDepartmentCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load( ['js/controllers/adminDepartmentCtrl.js'] );
                          }]
                  }
              })
                //权限列表
              .state('app.admin.auth', {
                  url: '/auth',
                  templateUrl: 'tpl/admin/authority_list.html',
                  controller:"adminAuthorityCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return $ocLazyLoad.load('ui.tree')
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/adminAuthorityCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })

              // 工单管理
              .state('app.order', {
                  url: '/order',
                  template: '<div ui-view></div>'
              })
              //工单获取
              .state('app.order.get', {
                  url: '/get',
                  templateUrl: 'tpl/word_order/order_get.html',
                  controller:"workOrderGet",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return $ocLazyLoad.load('ngFileUpload')
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/workOrderGet.js'] );
                                  }
                              );
                          }]
                  }
              })
              //工单分配
              .state('app.order.assign', {
                  url: '/assign',
                  templateUrl: 'tpl/word_order/order_assign.html',
                  controller:"workOrderAssign",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load( ['js/controllers/workOrderAssign.js'] );
                          }]
                  }
              })

              // 推送管理
              .state('app.push', {
                  url: '/push',
                  template: '<div ui-view></div>'
              })
              //推送列表
              .state('app.push.list', {
                  url: '/list',
                  templateUrl: 'tpl/push/push_list.html',
                  controller:"pushListCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad, $ocLazyLoad ){
                              return uiLoad.load(JQ_CONFIG['datetimepicker'])
                                  .then($ocLazyLoad.load('ngFileUpload'))
                                  .then($ocLazyLoad.load('multi-select-tree'))
                                  .then(function () {
                                  return uiLoad.load( ['js/controllers/pushListCtrl.js'] );
                              })
                          }]
                  }
              })
              //推送列表
              .state('app.push.sms', {
                  url: '/sms',
                  templateUrl: 'tpl/push/push_sms.html',
                  controller:"smsCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load( ['js/controllers/smsCtrl.js'] );
                          }]
                  }
              })
              //资讯管理
              .state('app.info', {
                  url: '/info',
                  template: '<div ui-view></div>'
              })
              //文章列表
              .state('app.info.information', {
                  url: '/information',
                  templateUrl: 'tpl/info/information_list.html',
                  controller:"informationCtrl",
                  resolve: {
                      deps: ['uiLoad','$ocLazyLoad',
                          function( uiLoad,$ocLazyLoad){
                              return $ocLazyLoad.load('ngFileUpload')
                                  .then(function(){
                                      return uiLoad.load( ['js/controllers/informationCtrl.js'] );
                                  }
                              );
                          }]
                  }
              })
              //媒体列表
              .state('app.info.media', {
                  url: '/media',
                  templateUrl: 'tpl/info/media_list.html',
                  controller:"infoMedia",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load( ['js/controllers/infoMedia.js'] );
                          }]
                  }
              })
              //直播列表
              .state('app.info.broadcast', {
                  url: '/broadcast',
                  templateUrl: 'tpl/info/broadcast_list.html',
                  controller:"infoBroadcastCtrl",
                  resolve: {
                      deps: ['uiLoad',
                          function( uiLoad ){
                              return uiLoad.load( ['js/controllers/infoBroadcastCtrl.js'] );
                          }]
                  }
              })
              // others
              .state('lockme', {
                  url: '/lockme',
                  templateUrl: 'tpl/page_lockme.html'
              })
              .state('access', {
                  url: '/access',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('access.signin', {
                  url: '/signin',
                  templateUrl: 'tpl/page_signin.html',
                  resolve: {
                      deps: ['uiLoad',
                        function( uiLoad ){
                          return uiLoad.load( ['js/controllers/signin.js'] );
                      }]
                  }
              })
              .state('access.signup', {
                  url: '/signup',
                  templateUrl: 'tpl/page_signup.html',
                  resolve: {
                      deps: ['uiLoad',
                        function( uiLoad ){
                          return uiLoad.load( ['js/controllers/signup.js'] );
                      }]
                  }
              })
              .state('access.forgotpwd', {
                  url: '/forgotpwd',
                  templateUrl: 'tpl/page_forgotpwd.html'
              })
              .state('access.404', {
                  url: '/404',
                  templateUrl: 'tpl/page_404.html'
              })
              .state('layout', {
                  abstract: true,
                  url: '/layout',
                  templateUrl: 'tpl/layout.html'
              })
              .state('layout.fullwidth', {
                  url: '/fullwidth',
                  views: {
                      '': {
                          templateUrl: 'tpl/layout_fullwidth.html'
                      },
                      'footer': {
                          templateUrl: 'tpl/layout_footer_fullwidth.html'
                      }
                  },
                  resolve: {
                      deps: ['uiLoad',
                        function( uiLoad ){
                          return uiLoad.load( ['js/controllers/vectormap.js'] );
                      }]
                  }
              })

      }
    ]
  );